﻿using api.Plugins;
using DataProvider;
using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Mvc;
using Model;
using Newtonsoft.Json;
using Service;
using System;
using Utility;

namespace api.Controllers
{
    [Route("api/[controller]/[action]")]
    [ApiController]
    [Authorize]
    public class Sys_DeptController : ControllerBase
    {
        public IActionResult GetTreeList(LayPageModel param)
        {
            var List = Sys_DeptProvider.Instances.GetTreeList();
            return Ok(JsonConvert.SerializeObject(new ResultData<object>() { code = 0, count = List.Count, data = List })) ;
        }
        public IActionResult Save(CommonSaveModel param)
        {
            var userToken = this.GetUserToken();
            var Ent= JsonConvert.DeserializeObject<Sys_Dept>(param.strEntity);
            if(Ent.Id==0)
            {
                Ent.CreateUser = userToken.UserId;
                Ent.CreateTime = DateTime.Now;
                Sys_DeptService.Instances.Add(userToken.UserId, Ent);
            }
            else
            {
                Ent.LastUpdateTime = DateTime.Now;
                Ent.LastUpdateUser = userToken.UserId;
                Sys_DeptService.Instances.Edit(userToken.UserId, Ent);
            }
            return Ok(JsonConvert.SerializeObject(new ResultData<object>() { code = 0, data="Success" }));
        }
        public IActionResult Del(CommonIds param)
        {
            var userToken = this.GetUserToken();
            var List = Sys_DeptService.Instances.GetListByIds(param.Ids);
            foreach(var d in List)
            {
                d.LastUpdateTime = DateTime.Now;
                d.LastUpdateUser = userToken.UserId;
                d.Status = Status.delete;
              
            }
            Sys_DeptService.Instances.EditBatch(userToken.UserId, List);
            return Ok(JsonConvert.SerializeObject(new ResultData<object>() { code = 0, data = "Delete Success" }));
        }
    }
}
